//
//  UserAccountViewModel.swift
//  weibo22
//
//  Created by keshuaiguo on 16/1/19.
//  Copyright © 2016年 keshuaiguo. All rights reserved.
//

import UIKit
import AFNetworking

class UserAccountViewModel: NSObject {

    
    var userAccount: UserAccount?
    override init() {
        //缓存的数据  给模型赋值
        userAccount = UserAccount.loadUserAccount()
        
       
        super.init()
    }
    //用户是否登录的标记
    //只读
    var userLogin: Bool {
    
        return userAccount?.access_token != nil
        
    }
    //从缓存获取token
    var token: String? {
        return userAccount?.access_token
    }
    //获取授权的token
    func loadAccessToken(code:String,finished: (isSuccess : Bool)->()) {
        
        
        let urlString = "https://api.weibo.com/oauth2/access_token"
        //参数
        let parameters = ["client_id" : client_id,"client_secret" : client_secret,"grant_type" : "authorization_code","code" : code,"redirect_uri" : redirect_uri]
        let AFN = AFHTTPSessionManager()
        //设置支持解析json的格式
        AFN.responseSerializer.acceptableContentTypes?.insert("text/plain")
        AFN.POST(urlString, parameters: parameters, progress: nil, success: { (_, result) -> Void in
            
//            print(result)
            //类型转换 angObject - >[key : value]
            
            guard let dict = result as? [String : AnyObject] else{
                
                print("数据格式不合法")
                //执行失败的回调
                finished(isSuccess: false)
                return
            }
            //程序走到这  转换字典成功
            //            let access_token = dict["access_token"] as! String
            //
            //            let uid = dict["uid"]as! String
            
            let account = UserAccount(dict: dict)
//            print(account)
            //获取用户信息
            self.loadUserInfo(account, finished: finished)
            
            }) { (_, error) -> Void in
                finished(isSuccess: false)
                print(error)
        }
        
    }
    //MARK:加载用户信息
    private func loadUserInfo(account : UserAccount,finished: (isSuccess:Bool)->()) {
        
        
        //1.准备uil
        let uilString = "https://api.weibo.com/2/users/show.json"
        
        //2.获取AFN对象
        let AFN = AFHTTPSessionManager()
        let parameters = ["access_token":account.access_token!,"uid" : account.uid!]
        //3.调用GET
        AFN.GET(uilString, parameters: parameters, progress: nil, success: { (_, result) -> Void in
            
//            print(result)
            //获取用户信息数据
            //类型转换 angObject - >[key : value]
            guard let dict = result as? [String : AnyObject] else {
                
                finished(isSuccess: false)
                print("数据格式错误")
                
                return
            }
            
            let name = dict["name"] as! String
            let avatar_large = dict["avatar_large"] as! String
            //需要的用户信息
            account.name = name
            account.avatar_large = avatar_large
           
            //保存数据到沙盒
            account.saveAccount()
            //执行成功的回调
            finished(isSuccess: true)
            }) { (_, error) -> Void in
                finished(isSuccess: false)
                print(error)
        }
    }

}
